package hczc.longcai.com.hccrowdfunding.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;

/**
 * 数据库操作类
 * Created by Administrator on 2016/2/1.
 */
public class DBAddressController {

    private static final String TAG = "DBAddressController";


    private Context context;
    private static DBHelper helper;
    private static SQLiteDatabase db;

    private List<Address> list;
    private ContentValues contentValues;

    public DBAddressController(Context context) {
        this.context = context;
        helper = new DBHelper(this.context);
        db = helper.getReadableDatabase();
    }



    /**
     * 获取数据库中的地址
     * @return 数据库中所有地址的集合
     */

    public List<Address> getAddress() {
        list = new ArrayList<>();
        Address info = null;
        Cursor cursor = db.query("address", null, null, null, null, null, "aDefault");
        if (cursor != null) {
            while (cursor.moveToNext()) {
                boolean bDafault = false;
                int index = cursor.getPosition();
//                Log.d(TAG, "当前id=" + index);
                int id = cursor.getInt(cursor.getColumnIndex("_id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String phone = cursor.getString(cursor.getColumnIndex("phone"));
                String area = cursor.getString(cursor.getColumnIndex("area"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
                int aDefault = cursor.getInt(cursor.getColumnIndex("aDefault"));
                if (aDefault == 0) {
                    bDafault = true;
                }
                info = new Address(index, id, name, phone, area, address, bDafault);
                list.add(info);
            }
            cursor.close();
        } else
            list = null;
        return list;
    }

    /**
     * 根据id删除数据库中的地址
     * @param id
     * @return 删除是否成功
     */
    public boolean deleteAddress(int id) {
        boolean flag = false;
        int delete = db.delete("address", "_id = ?", new String[]{String.valueOf(id)});
        Log.d(TAG, "成功删除了" + delete + "条数据");
        if (delete != 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * 更新数据库中的地址
     * @param address 需要更新的地址信息
     */
    public void updateAddress(Address address) {
        int aDfault = 1;
        int id = address.getId();
        if (contentValues == null) {
            contentValues = new ContentValues();
        }
        if (address.isDfault()) {
//            Log.d(TAG, "*************************");
            updateDefault();
            aDfault = 0;
        }
        contentValues.put("name", address.getName());
        contentValues.put("phone", address.getPhone());
        contentValues.put("area", address.getArea());
        contentValues.put("address", address.getAddress());
        contentValues.put("aDefault", aDfault);

        Log.d(TAG, contentValues.toString());

        int update = db.update("address", contentValues, "_id = ?", new String[]{String.valueOf(id)});
        Log.d(TAG, "更新了" + update + "条数据");
        contentValues.clear();
    }


    /**
     * 向数据库中添加地址
     * @param address 需要添加的地址信息
     * @return
     */
    public boolean addAddress(Address address) {
        boolean flag = true;
        int aDfault = 1;
        if (address.isDfault()) {
            updateDefault();
            aDfault = 0;
        }
        if (contentValues == null) {
            contentValues = new ContentValues();
        }
        contentValues.putNull("_id");
        contentValues.put("name", address.getName());
        contentValues.put("phone", address.getPhone());
        contentValues.put("area", address.getArea());
        contentValues.put("address", address.getAddress());
        contentValues.put("aDefault", aDfault);
        long lines = db.insert("address", null, contentValues);
        Log.d(TAG, "共插入了" + lines + "行");
        contentValues.clear();
        return flag;
    }

    /**
     * 修改地址的默认值
     */

    public void updateDefault() {
        if (contentValues == null) {
            contentValues = new ContentValues();
        }
        int aDefault = 1;
        contentValues.put("aDefault", aDefault);
        int update = db.update("address", contentValues, "aDefault = ?", new String[]{"0"});
        Log.d(TAG, "成功修改默认值，修改了 " + update + "条");
        contentValues.clear();
    }


    public void closeDB() {
        db.close();
    }


}
